package com.cl.park.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.cl.park.dao.BaseDao;
import com.cl.park.model.Car;
import com.cl.park.model.Vip;
import com.cl.park.service.CarService;

public class CarServiceImpl implements CarService{

	public BaseDao baseDao;
	
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	
	public String addOrUpdateCar(Car car) {
		if(car.getId()!=null){
			this.baseDao.update(car);
		}else{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			car.setLastTime(format.format(new Date()));
			this.baseDao.add(car);
		}
		return "ok";
	}

	public List<Object> listCar(Car car) {
		String hql = "from Car where 1=1";
		if(null != car){
			if(car.getCarNo()!=null && !car.getCarNo().equals("")){
				hql+=" and carNo like '%"+car.getCarNo()+"%'";
			}
			if(car.getCartYpe()!=null){
				hql+=" and cartYpe = "+car.getCartYpe();
			}
		}
		List<Object> list = this.baseDao.getListByHql(hql);
		return list;
	}

	public String vipCar(Vip vip, Car car) {
		
		vip = (Vip)this.baseDao.getByNProperty(Vip.class, "phone",vip.getPhone());
		if(vip==null){
			return "none";
		}
		car = (Car)this.baseDao.get(Car.class, car.getId());
		car.setVipNo(vip.getId());
		this.baseDao.update(car);
		return "ok";
	}
	
	public Car getById(Integer id){
		return (Car) baseDao.get(Car.class, id);
	}

}
